<?php

namespace App\Utils;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Carbon\Carbon;
class Utils
{
    public static function generateRandomHeaders()
    {
        $headers = [];

        // 随机生成 User-Agent
        $headers['User-Agent'] = self::generateRandomUserAgent();

        // // 随机生成 Accept
        // $headers['Accept'] = self::generateRandomAccept();

        // // 随机生成 Accept-Language
        // $headers['Accept-Language'] = self::generateRandomAcceptLanguage();

        // // 随机生成 Accept-Encoding
        // $headers['Accept-Encoding'] = self::generateRandomAcceptEncoding();

        // // 随机决定是否添加 Cookie
        // if (rand(0, 1)) {
        //     $headers['Cookie'] = self::generateRandomCookie();
        // }

        // // 随机决定是否添加 Referer
        // if (rand(0, 1)) {
        //     $headers['Referer'] = self::generateRandomReferer();
        // }

        // // 其他常见固定请求头
        // $headers['Connection'] = self::getRandomConnection();

        return $headers;
    }

    private static function generateRandomUserAgent()
    {
        $browserTypes = ['Chrome', 'Firefox', 'Safari', 'Edge'];
        $browser = $browserTypes[array_rand($browserTypes)];
        $osTypes = ['Windows NT 10.0', 'Macintosh; Intel Mac OS X 10_15_7', 'Linux x86_64'];
        $os = $osTypes[array_rand($osTypes)];

        switch ($browser) {
            case 'Chrome':
                $version = rand(80, 110). '.' . rand(0, 9999) . '.' . rand(0, 999);
                return "Mozilla/5.0 ($os) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$version Safari/537.36";
            case 'Firefox':
                $version = rand(70, 100) . '.' . rand(0, 9);
                return "Mozilla/5.0 ($os; rv:$version) Gecko/20100101 Firefox/$version";
            case 'Safari':
                $version = rand(14, 16) . '.' . rand(0, 9) . '.' . rand(0, 9);
                return "Mozilla/5.0 ($os) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/$version Safari/605.1.15";
            case 'Edge':
                $version = rand(80, 110). '.' . rand(0, 9999) . '.' . rand(0, 999);
                return "Mozilla/5.0 ($os) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$version Safari/537.36 Edg/$version";
        }
    }

    private static function generateRandomAccept()
    {
        $acceptTypes = [
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed - exchange;v=b3;q=0.9',
            'application/json, text/plain, */*',
            'application/xml, text/xml, */*;q=0.5',
            'image/*',
        ];
        return $acceptTypes[array_rand($acceptTypes)];
    }

    private static function generateRandomAcceptLanguage()
    {
        $languages = ['en-US', 'zh-CN', 'fr-FR', 'de-DE', 'es-ES'];
        $primaryLang = $languages[array_rand($languages)];
        $secondaryLang = $languages[array_rand($languages)];
        while ($secondaryLang === $primaryLang) {
            $secondaryLang = $languages[array_rand($languages)];
        }
        return "$primaryLang, $secondaryLang;q=0.9";
    }

    private static function generateRandomAcceptEncoding()
    {
        $encodings = ['gzip', 'deflate', 'br'];
        $selectedEncodings = [];
        $count = rand(1, count($encodings));
        for ($i = 0; $i < $count; $i++) {
            $encoding = $encodings[array_rand($encodings)];
            if (!in_array($encoding, $selectedEncodings)) {
                $selectedEncodings[] = $encoding;
            }
        }
        return implode(', ', $selectedEncodings);
    }

    private static function generateRandomCookie()
    {
        $cookieNames = ['session_id', 'user_id', 'token', 'preferences'];
        $cookieValues = [
            self::generateRandomString(10),
            self::generateRandomString(20),
            self::generateRandomString(32),
        ];
        $cookies = [];
        $count = rand(1, 3);
        for ($i = 0; $i < $count; $i++) {
            $name = $cookieNames[array_rand($cookieNames)];
            $value = $cookieValues[array_rand($cookieValues)];
            $cookies[] = "$name=$value";
        }
        return implode('; ', $cookies);
    }

    private static function generateRandomReferer()
    {
        $domains = ['example.com', 'testsite.net', 'anotherpage.org'];
        $paths = ['/page1', '/section2/article', '/category/item'];
        $domain = $domains[array_rand($domains)];
        $path = $paths[array_rand($paths)];
        return "https://$domain$path";
    }

    private static function getRandomConnection()
    {
        $connections = ['keep-alive', 'close'];
        return $connections[array_rand($connections)];
    }

    private static function generateRandomString($length)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $string;
    }
    public static function getYMDDate()
    {
        $date = Carbon::now()->format('Y-m-d');
        return $date;
    }

    public static function getFormatted_datetime_now(){
        $date = Carbon::now()->format('Y-m-d H:i:s');
        return $date;
    }


    public static function  convertSelfEvaluate($selfEvaluate){
        switch($selfEvaluate){
            case '一般':
                return 1;
            case '良好':
                return 2;
            case '优秀':
                return 3;  
            case '尚需努力':
                return 4;    
            default:
                return 1;
        }
    }
    public static function convertRecordType($recordType){
        switch($recordType){
            case '日报':
                return 1;
            case '周报':
                return 2;
            case '月报':
                return 3;
            default:
                return 1;
        }
    }

    public static function CvanSelfEvaluateToHtml($selfEvaluate){   
        switch($selfEvaluate){
            case 1:
                return ['icon' => 1, 'text' => '一般'];
            case 2:
                return ['icon' => 2, 'text' => '良好'];
            case 3:
                return ['icon' => 3, 'text' => '优秀'];
            case 4:
                return ['rate' => 0, 'text' => '尚需努力'];
        }
    }

    public static function CvanSelfEvaluateDbToHtml($selfEvaluate){   
        switch($selfEvaluate){
            case 1:
                return ['icon' => 2, 'text' => '一般'];
            case 2:
                return ['icon' => 3, 'text' => '良好'];
            case 3:
                return ['icon' => 4, 'text' => '优秀'];
            case 4:
                return ['rate' => 1, 'text' => '尚需努力'];
        }
    }
    public static function HtmlToCvanSelfEvaluate($selfEvaluate){
        switch($selfEvaluate){
            case 1:
                return 4;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            default:
                return 1;
        }
    }

    public static function getLogType($recordType){
        switch($recordType){
            case 'daily':
                return '1';
            case 'weekly':
                return '2';
            case 'monthly':
                return '3';
            case 'all':
                return '4';
            default:
                return '1';
        }
    }
}
